$(function () {
    var check_datagrid = $("#check_datagrid");
    var eck_dialog = $("#eck_dialog");
    var eck_form = $("#eck_form");
    check_datagrid.datagrid({
        url: '/check/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'id', title: '编号', width: 100},
            {
                field: 'employee', title: '姓名', width: 100, formatter: function (value) {
                return value ? value.username : "";
            }
            },
            {field: 'checkDate', title: '签到日期', width: 100},
            {field: 'checkTime', title: '签到时间', width: 100},
            {field: 'checkoutTime', title: '签退时间', width: 100},
            {
                field: 'checkState', title: '签到状态', width: 100, formatter: function (value) {
                return value ? "签到正常" : "<font color='red'>迟到</font>"
            }
            },
            {
                field: 'checkoutState', title: '签退状态', width: 100, formatter: function (value) {
                if (value == undefined){
                    return ''
                }else {
                    return value ? "签退正常" : "<font color='red'>早退</font>"
                }
            }
            },
            {field: 'repairTime', title: '补签时间', width: 100, sortable: true}
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#eck_btns",
        sortName: 'hireDate',
        sortOrder: 'desc',

    });
    eck_dialog.dialog({
        width: 300,
        height: 400,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            eck_form.form('clear');
        }
    });

    $.extend($.fn.validatebox.defaults.rules, {
        maxdate: {
            validator: function(value, param){
                var d1 = $.fn.datebox.defaults.parser(param[0]);
                console.log(d1);
                var d2 = $.fn.datebox.defaults.parser(value);
                console.log(d2);
                return d2<=d1;
            },
            message: '不能超过此日期'
        }
    });


    $("#start_date").datebox({
        onSelect : function(beginDate){
            $('#end_date').datebox().datebox('calendar').calendar({
                validator: function(date){
                    return beginDate<date;//<=
                }
            });
        }
    });


    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    })
    var methodObject = {

        check:function () {
            eck_form.form('submit', {
                url: '/check/check.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '签到', 'info')
                        methodObject.cancel();
                        check_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'info');
                    }
                }
            })

        },

        checkout:function () {
            eck_form.form('submit', {
                url: '/check/checkout.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '签退', 'info')
                        methodObject.cancel();
                        check_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'info');
                    }
                }
            })

        },


        supplement:function () {

            eck_form.form('submit', {
                url: '/check/supplement.do',
                onSubmit: function(param){
                    var isValid  =  $("#b1").datebox('isValid');
                    if (isValid ){
                       param.sup = $("[name=supplement]").val();
                   }
                   return isValid ;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '成功', 'info')
                        methodObject.cancel();
                        check_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },





        edit: function () {
            $("#password_tr").hide();
            var row = check_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            //回显角色数据
            $.get('/check/getRoleByEmployeeId.do', {checkId: row.id}, function (data) {
                $("#role_combobox").combobox('setValues', data);
            })
            row['dept.id'] = row.dept.id;
            eck_form.form('load', row
            );
            eck_dialog.dialog('setTitle', '编辑员工');
            eck_dialog.dialog('open');
        },
        changeState: function () {
            var row = check_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/check/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            check_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            check_datagrid.datagrid('reload');
        },



        cancel: function () {
            eck_dialog.dialog('close');
        },



        query: function () {
            var keyword = $("[name=keyword]").val();
            var beginDate = $("[name=beginDate]").val();
            var endDate = $("[name=endDate]").val();
            check_datagrid.datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate:endDate
            })
        },
    }
})


